package com.financing.platform.web.api;

import java.util.Collections;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.financing.platform.common.Pager;
import com.financing.platform.common.ResultCode;
import com.financing.platform.common.ResultMap;
import com.financing.platform.common.ResultMap.STATUS;
import com.financing.platform.dto.FetchInformationDTO;
import com.financing.platform.service.IFetchInformationService;
import com.financing.platform.util.DTOdateSet;

@Controller("fetchInformationController")
@RequestMapping("/api/fetchInformation")
public class FetchInformationController extends BaseAction  { 

	private Logger logger = Logger.getLogger(getClass());
	
	@Autowired
	private IFetchInformationService fetchInformationService;
	
	@RequestMapping("/listFetchInformation")
	@ResponseBody
	public ResultMap list(HttpServletRequest request, FetchInformationDTO fetchInformation,Pager pager){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("query fetchInformation list begin .......");
			
			fetchInformation.setBegin(pager.getStartIndex());
			fetchInformation.setPageSize(pager.getPageSize());
			
			int count = fetchInformationService.queryListCount(fetchInformation);
			
			pager.setTotalCount(count);
			
			List<FetchInformationDTO> fetchInformationList = null;
			
			if(count > 0){
				fetchInformationList = fetchInformationService.queryList(fetchInformation);
			}else{
				fetchInformationList = Collections.emptyList();
			}
			result.setAttribute("pager", pager);
			result.setAttribute("list", fetchInformationList);
			result.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("query fetchInformation list end .......");
		}catch(Exception e){
			log.error("query fetchInformation error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
		
		return result;
	}
	
	@RequestMapping("/addFetchInformation")
	@ResponseBody
	public ResultMap add(HttpServletRequest request, FetchInformationDTO fetchInformation){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("add fetchInformation  begin .......");
			
//			DTOdateSetUtil.setCreateInfo(fetchInformation);
//			DTOdateSetUtil.setUpdateInfo(fetchInformation);
			
			int id = fetchInformationService.insert(fetchInformation);
			result.setAttribute("id", id);
			
			result.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("add fetchInformation list end .......");
		}catch(Exception e){
			log.error("add fetchInformation error!",e);
			result.setStatus(STATUS.FAILE);
			result.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
		
		return result;
	}
	
	
	@RequestMapping("/detailFetchInformation")
	@ResponseBody
	public ResultMap detail(HttpServletRequest request, FetchInformationDTO fetchInformation){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("query detail fetchInformation  begin .......");
			
			Integer id = fetchInformation.getId();
			
			if(id==null){
				result.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			
			FetchInformationDTO dto = fetchInformationService.loadById(id);
			
			result.setAttribute("dto",dto);
			
			result.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("query detail fetchInformation  end .......");
		}catch(Exception e){
			log.error("query detail fetchInformation error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
		
		return result;
	}
	
	@RequestMapping("/deleteFetchInformation")
	@ResponseBody
	public ResultMap delete(HttpServletRequest request, FetchInformationDTO fetchInformation){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("delete fetchInformation  begin .......");
			
			Integer id = fetchInformation.getId();
			
			if(id==null){
				result.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			
			fetchInformationService.delete(id);
			
			result.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("delete fetchInformation  end .......");
		}catch(Exception e){
			log.error("delete fetchInformation error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
		
		return result;
	}
	
	@RequestMapping("/updateFetchInformation")
	@ResponseBody
	public ResultMap update(HttpServletRequest request, FetchInformationDTO fetchInformation){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("update fetchInformation  begin .......");
			
			Integer id = fetchInformation.getId();
			
			if(id==null){
				result.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			
			DTOdateSet.setUpdateInfo(fetchInformation, request.getSession());
			
			fetchInformationService.update(fetchInformation);
			
			result.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("update fetchInformation list end .......");
		}catch(Exception e){
			log.error("update fetchInformation error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
		
		return result;
	}
	
	@RequestMapping("/updateForceFetchInformation")
	@ResponseBody
	public ResultMap updateForce(HttpServletRequest request, FetchInformationDTO fetchInformation){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("updateForce fetchInformation  begin .......");
			
			Integer id = fetchInformation.getId();
			
			if(id==null){
				result.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			
			DTOdateSet.setUpdateInfo(fetchInformation, request.getSession());
			
			fetchInformationService.updateForce(fetchInformation);
			
			
			result.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("updateForce fetchInformation list end .......");
		}catch(Exception e){
			log.error("updateForce fetchInformation error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
		
		return result;
	}
	
}
